// pages/car/car.js
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    cartTitle:"",
     cart:[],
     isEditor:false,
     selectedAll:false,
     totalPrice:0,
     foods:[]
  },

  onLoad: function (options) {
    if (wx.getStorageSync('shopcar').length !== 0) {
      this.setData({
        cart: wx.getStorageSync('shopcar')
      })
      //console.log(wx.getStorageSync('shopcar'), "9999998")
    }
    var that = this;
    wx.request({
      url: app.globalData.directory + 'api/v2/getGoodsAll',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        //console.log('111',res.data.status);
       // console.log('222',wx.getStorageSync('shopcar')
        
        if (res.data.status == 'ok') {
          res.data.data.forEach(function (item, index) {
            that.data.cart.forEach(function (itemm) {
              if (item.id == itemm.foodid) {
                itemm.foodindex = index;
                itemm.selected = true;
                item.goods.forEach(function (items, indexs) {
                  if (items.id == itemm.goodid) {
                    itemm.goodindex = indexs;
                  }
                })
              }
            })
          })
          that.setData({
            cart: that.data.cart,
            foods: res.data.data,
            totalPrice: wx.getStorageSync('priceszong')
          })
        } else {
          wx.showToast({
            title: '请求失败,请重试',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
    /*店铺介绍*/
    wx.request({
      url: app.globalData.directory + 'api/v2/getshopinfo', //仅为示例，并非真实的接口地址
      data: {
      },
      header: {
        'content-type': 'application/json', // 默认值
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          that.setData({
            cartTitle: res.data.data.name
          })
        } else {
          wx.showToast({
            title: '请求失败,请稍候',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    });
    that.getTotalPrice();

  },





  /**/ 
  editor: function (e) {
    this.setData({
      isEditor: !this.data.isEditor,
    })
  },
  selected: function (e) {
    var that = this;
    const index = e.currentTarget.dataset.index;
    let cart = this.data.cart;
    var bol = true;
    const selected = cart[index].selected;
    cart[index].selected = !selected;
    cart.forEach(function (item) {
      if (!item.selected) {
        bol = false
      }
    })
    if (bol) {
      that.data.selectedAll = true;
    } else {
      that.data.selectedAll = false;
    }
    this.setData({
      cart: cart,
      selectedAll: that.data.selectedAll
    });
    this.getTotalPrice();
  },
  AllSelected: function (e) {
    let selectedAll = this.data.selectedAll;
    selectedAll = !selectedAll;
    let cart = this.data.cart;

    for (let i = 0; i < cart.length; i++) {
      cart[i].selected = selectedAll;
    }
    this.setData({
      selectedAll: selectedAll,
      cart: cart
    });
    this.getTotalPrice();
  },
  addCount: function (e) {
    const index = e.currentTarget.dataset.index;
    let cart = this.data.cart;
    let num = cart[index].num;
    num = num + 1;
    cart[index].num = num;
    this.setData({
      cart: cart
    });
    this.getTotalPrice();
  },
  downCount: function (e) {
    const index = e.currentTarget.dataset.index;
    let cart = this.data.cart;
    let num = cart[index].num;
    if (num < 2) {
      return false
    };
    num = num - 1;
    cart[index].num = num;
    this.setData({
      cart: cart
    });
    this.getTotalPrice();
  },
  delete: function (e) {
    var that = this;
    let cart = that.data.cart;
    let cartNow = [];
    wx.showModal({
      title: '提示',
      content: '确定删除',
      success: function (res) {
        if (res.confirm) {
          for (let i = 0; i < cart.length; i++) {
            if (cart[i].selected == false) {
              cartNow.push(cart[i]);
            }
          }
          that.setData({
            cart: cartNow,
          })
          that.getTotalPrice();
        }
      }
    })
  },
  getTotalPrice: function () {
    let cart = this.data.cart;
    let total = 0;
    wx.setStorageSync('shopcar', this.data.cart);
    for (let i = 0; i < cart.length; i++) {
      if (cart[i].selected) {
        total += cart[i].num * cart[i].price;
      }
    }
    this.setData({
      cart: cart,
      totalPrice: total
    });
  },
  buy: function (e) {
    var that = this;
    var cart = that.data.cart;
    var cart1 = [];
    cart.forEach(function (item, index) {
      if (item.selected) {
        cart1.push(item)
      }
    })
    if (cart1.length == 0) {
      wx.showToast({
        title: '请选择商品',
        icon: 'none',
        duration: 2000
      })
    }
    if (!app.globalData.user.nickname) {
      wx.switchTab({
        url: '../my/my',
      })
      return false;
    }
    wx.setStorageSync('cart', cart1);
    var ID = undefined
    wx.navigateTo({
      url: '../confirmorder/confirmorder?id=' + ID,
    })
  },
  onShow: function () {
    if (wx.getStorageSync('shopcar').length !== 0) {
      this.setData({
        cart: wx.getStorageSync('shopcar'),
        isEditor: false,
        totalPrice: wx.getStorageSync('priceszong')
      })
    }
    var that = this;
    wx.request({
      url: app.globalData.directory + 'api/v2/getGoodsAll',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          res.data.data.forEach(function (item, index) {
            that.data.cart.forEach(function (itemm) {
              if (item.id == itemm.foodid) {
                itemm.foodindex = index;
                itemm.selected = true;
                item.goods.forEach(function (items, indexs) {
                  if (items.id == itemm.goodid) {
                    itemm.goodindex = indexs;
                  }
                })
              }
            })
          })
          that.setData({
            cart: that.data.cart,
            foods: res.data.data,
            selectedAll: true,
            totalPrice: wx.getStorageSync('priceszong')
          })
        } else {
          wx.showToast({
            title: '请求失败,请重试',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
    that.getTotalPrice();
  }

})
